<template>
  <div class="leaderboard">
    <div class="leaderboard-header">
      <h3>排行榜</h3>
      <div class="time-filter">
        <button
            v-for="filter in timeFilters"
            :key="filter.value"
            :class="{ 'active': activeFilter === filter.value }"
            @click="activeFilter = filter.value"
        >
          {{ filter.label }}
        </button>
      </div>
    </div>

    <div class="leaderboard-list">
      <div
          v-for="(player, index) in filteredPlayers"
          :key="player.id"
          class="leaderboard-item"
          :class="{
          'first-place': index === 0,
          'second-place': index === 1,
          'third-place': index === 2
        }"
      >
        <div class="player-rank">{{ index + 1 }}</div>
        <div class="player-avatar">
          <img :src="player.avatar" :alt="player.username">
        </div>
        <div class="player-info">
          <div class="player-name">{{ player.username }}</div>
          <div class="player-level">Level {{ player.level }}</div>
        </div>
        <div class="player-score">
          <i class="fas fa-trophy"></i>
          {{ player.score }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useLeaderboard } from '../composables/useLeaderBoard'

const { players } = useLeaderboard()

const timeFilters = [
  { label: '日排行', value: 'daily' },
  { label: '月排行', value: 'weekly' },
  { label: '总排行', value: 'total' }
]

const activeFilter = ref('weekly')

const filteredPlayers = computed(() => {
  // 这里可以根据activeFilter过滤数据
  return [...players.value].sort((a, b) => b.score - a.score).slice(0, 10)
})
</script>

<style scoped>
.leaderboard {
  height: 100%;
  padding: 15px;
  color: white;
}

.leaderboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.leaderboard-header h3 {
  margin: 0;
  font-size: 1.2rem;
}

.time-filter {
  display: flex;
  gap: 5px;
}

.time-filter button {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #a5a5a5;
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.time-filter button.active {
  background: #4cc9f0;
  color: white;
}

.leaderboard-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.leaderboard-item {
  display: flex;
  align-items: center;
  padding: 10px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.05);
  transition: all 0.3s ease;
}

.leaderboard-item:hover {
  background: rgba(255, 255, 255, 0.1);
}

.leaderboard-item.first-place {
  background: linear-gradient(90deg, rgba(255, 215, 0, 0.1) 0%, transparent 100%);
  border-left: 3px solid gold;
}

.leaderboard-item.second-place {
  background: linear-gradient(90deg, rgba(192, 192, 192, 0.1) 0%, transparent 100%);
  border-left: 3px solid silver;
}

.leaderboard-item.third-place {
  background: linear-gradient(90deg, rgba(205, 127, 50, 0.1) 0%, transparent 100%);
  border-left: 3px solid #cd7f32;
}

.player-rank {
  width: 25px;
  text-align: center;
  font-weight: bold;
  color: #a5a5a5;
}

.first-place .player-rank,
.second-place .player-rank,
.third-place .player-rank {
  color: white;
}

.player-avatar {
  width: 40px;
  height: 40px;
  margin: 0 10px;
}

.player-avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.player-info {
  flex: 1;
}

.player-name {
  font-weight: bold;
}

.player-level {
  font-size: 0.8rem;
  color: #a5a5a5;
}

.player-score {
  font-weight: bold;
  color: #4cc9f0;
  display: flex;
  align-items: center;
  gap: 5px;
}

.player-score i {
  color: gold;
}
</style>
